package com.ruoyi.web.service.impl;

import com.ruoyi.web.mapper.DatabaseMapper;
import com.ruoyi.web.service.DatabaseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.Collections;
import java.util.List;
import java.util.Map;

@Service
public class DatabaseServiceImpl implements DatabaseService {

    @Autowired
    private DatabaseMapper databaseMapper;

    @Override
    public List<String> getAllDatabases() {
        return databaseMapper.getAllDatabases();
    }

    @Override
    public List<Map<String, String>> listAllTables(String dbName) {
        return databaseMapper.listAllTables(dbName);
    }

    @Override
    public List<Map<String, String>> listColumns(String dbName, String tableName) {
        return databaseMapper.listColumns(dbName, tableName);
    }

    @Override
    public boolean validateSql(String sql) {
        try {
            List list = databaseMapper.validateSql(sql);
            if (CollectionUtils.isEmpty(list)) {
                return false;
            }
        } catch (Exception e) {
            return false;
        }
        return true;
    }

    @Override
    public List<Map<String, Object>> generateChart(String sql) {
        try {
            List<Map<String, Object>> list = databaseMapper.execSql(sql);
            return list;
        } catch (Exception e) {
            return null;
        }
    }
}
